package com.ruin.tech.ahri.service.impl;

import com.ruin.tech.ahri.dao.NoticeMapper;
import com.ruin.tech.ahri.dao.UserMapper;
import com.ruin.tech.ahri.exception.BizException;
import com.ruin.tech.ahri.model.dto.*;
import com.ruin.tech.ahri.service.IMailService;
import com.ruin.tech.ahri.service.INoticeService;
import com.ruin.tech.ahri.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program ahri
 * @description: 公告编辑
 * @author: ruin
 * @create: 2019/11/28 16:03
 */

@Slf4j
@Service
public class NoticeServiceImpl implements INoticeService {

    private ConcurrentHashMap<String, ConcurrentHashMap<Integer,Long>> messageMap = new ConcurrentHashMap<>();

    @Autowired
    NoticeMapper noticeMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    IMailService mailService;

    @Override
    public void editNotice(Notice notice) {
        List<Notice> notices = noticeMapper.selectByExample(new NoticeExample());
        notice.setCreateTime(new Date());
        if(notices != null && notices.size()>0){
            NoticeExample example = new NoticeExample();
            if(notice.getId()!= null && notice.getId()>0){
                example.createCriteria().andIdEqualTo(notice.getId());
            }else{
                example.createCriteria().andIdEqualTo(notices.get(0).getId());
                notice.setId(notices.get(0).getId());
            }
            noticeMapper.updateByExampleSelective(notice,example);
        }else{
            noticeMapper.insert(notice);
        }

    }

    @Override
    public Notice findNotice() {
        List<Notice> notices = noticeMapper.selectByExample(new NoticeExample());
        if(notices.size()>0){
            return notices.get(0);
        }
        return null;

    }

    @Override
    public void sendIdCode(ChangePwdDTO changePwdDTO) throws BizException {
        String email = changePwdDTO.getEmail();
        String userName = changePwdDTO.getUserName();
        //验证是否存在该用户，且该用户的邮箱是否正确
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserNameEqualTo(userName);
        List<User> users = userMapper.selectByExample(userExample);
        if(users != null && users.size()>0){
            if(email.equals(users.get(0).getEmail())){
                int idCode = Utils.radomInt();
                setCache(email,idCode);
                String subject = "购房助手小程序验证码";
                String content = "修改密码的验证码为: "+idCode+" ,请不要告诉任何人！";
                mailService.sendMail(changePwdDTO.getEmail(),subject, content);

            }else{
                throw new BizException("用户: "+userName+" 下的邮箱不正确");
            }
        }else{
            throw new BizException("不存在用户名为: "+userName+" 的账户");
        }
    }

    //缓存验证码
    private void setCache(String email, int idCode){
        ConcurrentHashMap<Integer,Long> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put(idCode,System.currentTimeMillis());
        messageMap.put(email,concurrentHashMap);
    }

    //取验证码，并剔除超时的验证码5分钟
    @Override
    public Integer getCache(String email){
        Iterator<Map.Entry<String, ConcurrentHashMap<Integer,Long>>> it = messageMap.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, ConcurrentHashMap<Integer,Long>> entry = it.next();
            ConcurrentHashMap<Integer,Long> concurrentHashMap = entry.getValue();
            for(Long time : concurrentHashMap.values()){
                if(System.currentTimeMillis() - time > 300000){
                    it.remove();
                }
            }
        }

        ConcurrentHashMap<Integer,Long> concurrentHashMap = messageMap.get(email);
        Iterator<Map.Entry<Integer,Long>> map = concurrentHashMap.entrySet().iterator();
        while (map.hasNext()){
            return map.next().getKey();
        }
        return null;
    }
}
